package im.starchat.chat.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import im.starchat.chat.R;
import im.starchat.chat.activity.WebViewActivity;
import im.starchat.chat.rest.model.MsgBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by liuchao on 2/10/16.
 * 直播feed流适配器
 */
public class MsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private static final int TYPE_MSG = 1001;
    private static final int TYPE_EMPTY = 1002;
    private List<MsgBean> mList;

    public MsgAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<MsgBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void addAll(List<MsgBean> list){
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_EMPTY:
                return new EmptyHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_empty, parent, false));
            case TYPE_MSG:
            default:
                return new MsgViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_msg, parent, false));

        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_EMPTY:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                emptyHolder.mInfoText.setText("无新消息");
                break;
            case TYPE_MSG:
                MsgViewHolder msgViewHolder = (MsgViewHolder) holder;
                msgViewHolder.msgTitle.setText(mList.get(position).getTitle());
                msgViewHolder.msgBody.setText(mList.get(position).getBody() + " >>");
                msgViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent webIntent = new Intent();
                        webIntent.setClass(mContext, WebViewActivity.class);
                        webIntent.putExtra(WebViewActivity.WEBVIEW_TITLE_FLAG, mList.get(position).getTitle());
                        webIntent.putExtra(WebViewActivity.WEBVIEW_URL_FLAG, mList.get(position).getUrl());
                        mContext.startActivity(webIntent);
                    }
                });
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(mList == null || mList.size() == 0){
            return TYPE_EMPTY;
        }else{
            return TYPE_MSG;
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 1;
        }
        return mList.size();
    }

    public class MsgViewHolder extends RecyclerView.ViewHolder {


        @InjectView(R.id.msg_title)
        TextView msgTitle;
        @InjectView(R.id.msg_body)
        TextView msgBody;


        public MsgViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class EmptyHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.info_text)
        TextView mInfoText;

        public EmptyHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }


}
